﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input; 

namespace LP2
{
    class Ally:Creature
    {
        int hit;
        int typeCreature;
        float scale;
        int lastMouseX;
        Boolean colisionFlag;
        Boolean shootFlag;
        float angleShoot;
        int lastMouseY;
        float lastXBeforeColision;
        float lastYBeforeColision;
        Timer timer;
        float timerBeginX;
        float timerBeginY;
        Boolean timerFlagStop;
        Boolean FlagColisionEnemyDestroy=false;
        public Boolean FlagDraw = true;
        
        int numberColision; //Variable que ayudará a verificar la primera colisión con algún objeto
                            //Se volverá a setear a 0 cuando salga del estado colision
        

        public Ally(CreaturePropertyConfig cpc,int typeCreature,Shooter s,List<Ally> listAlly)
        {
            if (listAlly.Count() == 0)
                position = new Vector2(s.getPosition().X, s.getPosition().Y ); //posicion inicial de disparo //datos cambiaran en archivo configurable **Properties
            else
                position = new Vector2(listAlly[listAlly.Count()-1].getPositionX()-30, s.getPosition().Y + s.getAltitud());
            
            
            init_position = position;
            lastMouseX = -1;
            lastMouseY = -1;
            this.colisionFlag = false;
            this.shootFlag = false;
            this.numberColision = 0;
            
            scale = 0.5f;
            //Dependiendo del tipo de atacante

            this.HP = cpc.getAllyHPList()[typeCreature-1];
            this.hit = cpc.getAllyHITList()[typeCreature-1];
            this.masa = cpc.getAllyMASAList()[typeCreature-1];
            this.typeCreature=typeCreature;
            timer = new Timer();
            timer.Interval = 1000;
            timer.Elapsed += new ElapsedEventHandler(OnTimerTick);
            timerFlagStop = false;
            
        }

        public int getHit()
        {
            return this.hit;
        }

        public void setHit(int hit)
        {
            this.hit = hit;
        }


        public float getPositionX()
        {
            return this.position.X;
        }

        public float getPositionY()
        {
            return this.position.Y;
        }

        public Boolean getColisionFlag()
        {
            return this.colisionFlag;
        }

        public void setColisionFlag(Boolean colisionFlag)
        {
            this.colisionFlag = colisionFlag;
        }

        public int getNumberColision()
        {
            return this.numberColision;
        }

        public void setNumberColision(int n)
        {
            this.numberColision = n;
        }

        public void incNumberColision()
        {
            this.numberColision++;
        }


        public void setAngle(float angle)
        {
            this.angleShoot = angle;
        }

        public float getAngle()
        {
            return this.angleShoot;
        }

        private void OnTimerTick(object sender, EventArgs e)
        {
            if ((this.position.X == timerBeginX) && (this.position.Y == timerBeginY))
            {
                timer.Stop();
                timer.Close();
                timer.Dispose();
                timerFlagStop = true;
            }
        }


        public void setFlagColisionEnemyDestroy(Boolean f)
        {
            this.FlagColisionEnemyDestroy = f;
        }

        public Boolean ClickOnMe(MouseState m)
        {
            Boolean x = false;

            if (m.LeftButton == ButtonState.Pressed)
            {
                    x = this.position.X <= m.X &&
                         this.position.X + this.imagen.Width >= m.X &&
                         this.position.Y <= m.Y &&
                         this.position.Y + this.imagen.Height >= m.Y;
            }
            return x;
        }

        public void MoveWithClick(MouseState m,Shooter s)
        {   
            if ((lastMouseX!=m.X || lastMouseY!=m.Y) && (lastMouseX!=-1 && lastMouseY!=-1))
            {   
                if ((Math.Pow((m.X - this.init_position.X), 2) + Math.Pow((m.Y - this.init_position.Y), 2) <= Math.Pow(s.getRadius(), 2)))
                    position = new Vector2(this.position.X + m.X - lastMouseX ,
                                            this.position.Y + m.Y - lastMouseY);

            }
            lastMouseX = m.X;
            lastMouseY = m.Y;
        }

        public Boolean StopBird(Game g)
        {

            if (this.position.X > g.graphics.PreferredBackBufferWidth || this.position.Y > g.graphics.PreferredBackBufferHeight
                || this.position.Y <-this.imagen.Width || this.position.X<-this.imagen.Height)
            {   
                return true;
            }

            if (timerFlagStop)
            {
                return true;
            }

            return false;
        }


        public void ShotBird(MouseState m,float t,Shooter s,Physic phy)
        {
            if (!shootFlag )
            {
                angleShoot = phy.CalculusAngle(m.Y, this.init_position.Y, m.X, this.init_position.X);
                phy.CalculusV0(s, m,angleShoot);
                shootFlag=true;
                timer.Start();
            }
            else
            {
                //Guardamos el recorrido en X-Y antes de alguna colisión
                if (numberColision == 0)
                {
                    this.lastXBeforeColision = this.position.X;
                    this.lastYBeforeColision = this.position.Y;
                }
            }

            if ((numberColision == 1) && (!FlagColisionEnemyDestroy))
            {
                angleShoot = phy.CalculusAngle(this.position.Y, lastYBeforeColision,
                                                this.position.X, lastXBeforeColision);
                this.position.X = lastXBeforeColision;
                this.position.Y = lastYBeforeColision;
                this.timerBeginX = this.position.X;
                this.timerBeginY = this.position.Y;
                numberColision++;
                angleShoot = angleShoot * 0.5f;
                FlagColisionEnemyDestroy = false;
            }

            this.position = phy.MoveParabolico(angleShoot, t, this.position.X, this.position.Y, s.getV0X(),s.getV0Y());
        }


        public void IncrementaFuerza(int cantFuerzaIncrementar)
        {
            this.hit += cantFuerzaIncrementar;
            this.HP = this.HP*2; //Se incrementa en un 50% su vida
        }

            //Metodo para incrementar el tamaño del atacante al doble
        public void IncrementarTamano()
        {
            this.scale = 1.5f;
        }

        public void changeToMetallic()
        {

        }

        public void LoadContent(ContentManager Content)
        {
            //NEW : Seteamos la imagen dependiendo del tipo de atacante
                imagen = Content.Load<Texture2D>("ally"+typeCreature);
                

        }

        public void Draw(SpriteBatch spbtch)
        {   
            if (scale != 1)
            {
                spbtch.Draw(imagen, position, null, Color.White, 0f,new Vector2(0,0),scale, SpriteEffects.None, 0f);
            }
            else
            {
                spbtch.Draw(imagen, position, Color.White);
            }
        } 
    }
}
